import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path';
import fs from 'fs';

const alias = {
  "@": path.resolve(__dirname, "./src")
};

export default defineConfig(async ({ command, mode }) => {
  console.log('系统启动：',command, mode)
  if (command !== 'serve') {
    fs.readFile('./package.json', (err, data) =>{
      if (err) {
        console.error(err,'packageJSON读取出错！');
        return;
      }
      const jsonData = JSON.parse(data);
      const nowTime = new Date();
      jsonData.nversion = "V1.0-"+nowTime.getFullYear()+"."+(nowTime.getMonth()+1)+"."+nowTime.getDate()+"."+nowTime.getHours();
      let jsonStr = JSON.stringify(jsonData)
      fs.writeFile('./package.json', jsonStr, (err) =>{
        if (err) {
          console.error(err,'packageJSON写入出错！');
          return;
        }
        console.log('packageJSON写入成功！');
      });
      // 处理读取到的文件数据
    });
  }
  return {
    resolve: {
      alias
    },
    esbuild: {
      charset: 'ascii'
    },
    server: {
      port: 8080,
    },
    plugins: [
      vue(),
      vueJsx(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/icons')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]'
      })
    ]
  }
})
